#include <stdio.h>
#include <omp.h>

int main() {
    int x = 1;
    
    printf("Initial x = %d\n", x);
    printf("Running producer-consumer tasks...\n");

    #pragma omp parallel
    #pragma omp single
    {
        // 生产者任务（必须最先执行）
        #pragma omp task shared(x) depend(out: x)  // T1
        {
            x = 2;
            printf("Producer (Thread %d): Set x = %d\n", omp_get_thread_num(), x);
        }
        
        // 消费者任务1（可并行）
        #pragma omp task shared(x) depend(in: x)   // T2
        {
            printf("Consumer 1 (Thread %d): x+1 = %d\n", omp_get_thread_num(), x + 1);
        }
        
        // 消费者任务2（可并行）
        #pragma omp task shared(x) depend(in: x)   // T3
        {
            printf("Consumer 2 (Thread %d): x+2 = %d\n", omp_get_thread_num(), x + 2);
        }
    }

    printf("Final x = %d\n", x);
    return 0;
}